cmake_minimum_required(VERSION 3.16)

project(WingHexAsm LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_INCLUDE_CURRENT_DIR TRUE)

if(POLICY CMP0148)
    set(CMAKE_POLICY_DEFAULT_CMP0148 OLD)
endif()

if(MSVC)
    string(APPEND CMAKE_CXX_FLAGS " /utf-8")
    string(APPEND CMAKE_C_FLAGS " /utf-8")
endif()

option(TEST_MODE OFF)

add_subdirectory(WingPlugin)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools)

set(TS_FILES WingHexAsm_zh_CN.ts WingHexAsm_zh_TW.ts)

set(TRANSLATION_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src")

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if(POLICY CMP0063)
    set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
endif()

add_subdirectory(3rdparty/QCodeEditor)
add_subdirectory(3rdparty/capstone)
add_subdirectory(3rdparty/keystone)

# disable the fucking warnnings generated by 3rdparty
if(MSVC)
    target_compile_options(capstone PRIVATE "/wd4098;/wd4133;/wd4715;")

    file(GLOB files "3rdparty/keystone/suite/fuzz/fuzz*.c")
    foreach(file ${files})
        get_filename_component(FUZZ_TARGET ${file} NAME_WE)
        target_compile_options(${FUZZ_TARGET} PRIVATE "/wd4477;")
    endforeach()
else()
    target_compile_options(capstone PRIVATE -Wno-unused-private-field
                                            -Wno-unused-but-set-variable)
endif()

if(${QT_VERSION_MAJOR} EQUAL 5)
    qt5_create_translation(QM_FILES ${TRANSLATION_PATH} ${TS_FILES} OPTIONS
                           -no-obsolete)
elseif(${QT_VERSION_MAJOR} EQUAL 6)
    qt6_create_translation(QM_FILES ${TRANSLATION_PATH} ${TS_FILES} OPTIONS
                           -no-obsolete)
else()
    message(FATAL_ERROR "Unsupported QT version")
endif()

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/TranslationUtils.cmake)
add_plugin_translations_resource(QM_RES WingHexAsm ${QM_FILES})

add_library(
    WingHexAsm SHARED
    ${WINGHEX_SDK_HEADER}
    ${QM_FILES}
    ${QM_RES}
    src/winghexasm.cpp
    src/winghexasm.h
    src/WingHexAsm.json
    resources.qrc
    src/wingengine.h
    src/wingengine.cpp
    src/asmwindow.h
    src/asmwindow.cpp
    src/asmwindow.ui
    src/syntaxconfigparser.h
    src/syntaxconfigparser.cpp
    src/asmhighlighter.h
    src/asmhighlighter.cpp)

set_target_properties(WingHexAsm PROPERTIES SUFFIX ".wingplg")

if(TEST_MODE)
    if(NOT EXISTS "${WINGHEX_PATH}")
        message(
            FATAL_ERROR "Please config the WingHexExplorer2 Path - WINGHEX_PATH"
        )
    endif()

    set(WINGHEX_PLUGIN_PATH "${WINGHEX_PATH}/plugin")
    add_custom_command(
        TARGET WingHexAsm
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E make_directory ${WINGHEX_PLUGIN_PATH}
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:WingHexAsm>
                ${WINGHEX_PLUGIN_PATH})
endif()

target_link_libraries(
    WingHexAsm PRIVATE Qt${QT_VERSION_MAJOR}::Widgets WingPlugin QCodeEditor
                       capstone keystone)
